//
//  MAAuthorViewController.m
//  meiang_edu
//
//  Created by hfcb on 2018/9/14.
//  Copyright © 2018年 com.meiang.meiang_edu. All rights reserved.
//

#import "MAAuthorViewController.h"
#import "MAJiGouModel.h"
#import "MAFuncItemModel.h"
#import "MAStaffModel.h"

@interface MAAuthorViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;

/** staffModel */
@property(nonatomic,strong) MAStaffModel *staffModel;

@end

@implementation MAAuthorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasic];
    
    [self getUserData];
}

- (void)setupBasic {
    self.title = @"授权注册确认";
}

- (void)getUserData {
    [MBManager showLoading];
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    NSString *userID = [[self.scanResult componentsSeparatedByString:@","] lastObject];
    NSString *entity_type = [self.scanResult componentsSeparatedByString:@","][1];
    
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request) {
        request.requestSerializerType = ZBHTTPRequestSerializer;
        request.URLString = [NSString stringWithFormat:@"%@%@?entity_type=%@&entity_key=%@",BASE_ENTITY_INFO,userID,entity_type,userID];
        request.methodType = ZBMethodTypeGET;
        [request setValue:userinfo.token forHeaderField:@"token"];
        request.apiType = ZBRequestTypeRefresh;
    } success:^(id responseObject, apiType type) {
        [MBManager hideAlert];
        // 解析数据
        NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dataDict);
        NSInteger code = [dataDict[@"msgcode"] integerValue];
        if (code == 1) { // 请求成功
            self.staffModel = [MAStaffModel mj_objectWithKeyValues:dataDict[@"value"]];
            [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMG_OSS,self.staffModel.ps_id]]];
            self.nameLabel.text = self.staffModel.cname;
            self.phoneLabel.text = [[self.scanResult componentsSeparatedByString:@","] firstObject];
        } else {
            [MBManager showBriefAlert:dataDict[@"message"]];
        }
    } failure:^(NSError *error) {
        DLog(@"%@",error);
    } finished:^(id responseObject, apiType type, NSError *error, BOOL isCache) {
        if (isCache==YES) {
            DLog(@"使用了缓存");
        }
        [MBManager hideAlert];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)confirmClick {
    MAUserInfo *userinfo = [MAUserInfoTool userinfo];
    //    if (!userinfo.token) {
    //        [MBManager showBriefAlert:@"登录状态已过期,请重新登录"];
    //        return;
    //    }
    [MBManager showLoading];
//    "entity_type": "",关系类型  family_user,family_sender
//    "entity_id": "",//实体ID
//    "org_id": ""//当前机构ID

    NSMutableDictionary *body = [NSMutableDictionary dictionary];
    
    body[@"entity_type"] = self.funcItemModel.function_code;
    body[@"entity_id"] = [[self.scanResult componentsSeparatedByString:@","] lastObject];
    body[@"org_id"] = self.jigouModel.ID;
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:body options:0 error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSMutableURLRequest *req = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:BASE_LIST_ENABLE parameters:nil error:nil];
    [req setValue:@"application/text" forHTTPHeaderField:@"Content-Type"];
    [req setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [req setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    [[manager dataTaskWithRequest:req completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
        [MBManager hideAlert];
        if (!error) {
            DLog(@"Reply JSON: %@", responseObject);
            NSInteger msgcode = [responseObject[@"msgcode"] integerValue];
            NSString *message = responseObject[@"message"];
            if (msgcode == 1) {
                [MBManager showBriefAlert:message];
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                [MBManager showBriefAlert:message];
                return;
            }
        } else {
            DLog(@"Error: %@, %@, %@", error, response, responseObject);
            [MBManager showBriefAlert:@"网络错误"];
        }
    }] resume];
}
- (IBAction)cancelClick {
    [self.navigationController popViewControllerAnimated:YES];
}
@end
